﻿' Copyright (C) 2015 HTTPBuilder
'
' This program is free software: you can redistribute it and/or modify it under the +terms 
' of the GNU General Public License as published by the Free Software Foundation, either 
' version 3 of the License, or (at your option) any later version.

' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
' without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' See the GNU General Public License for more details.

' You should have received a copy of the GNU General Public License along with this program.
' If not, see http://www.gnu.org/licenses/. 

Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports HTTPBuilder.Interop

Namespace Controls
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Friend NotInheritable Class ListViewHelper
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        <StructLayout(LayoutKind.Sequential)> _
        Friend Structure LVGROUP
            Public cbSize As Integer
            Public mask As Integer
            <MarshalAs(UnmanagedType.LPTStr)> _
            Public pszHeader As String
            Public cchHeader As Integer
            <MarshalAs(UnmanagedType.LPTStr)> _
            Public pszFooter As String
            Public cchFooter As Integer
            Public iGroupId As Integer
            Public stateMask As Integer
            Public state As Integer
            Public uAlign As Integer
        End Structure
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Enum GroupState
            COLLAPSIBLE = 8
            COLLAPSED = 1
            EXPANDED = 0
        End Enum
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="lv"></param>
        ''' <param name="state"></param>
        ''' <remarks></remarks>
        Public Shared Sub SetGroupCollapse(lv As ListView, state As GroupState)
            For i As Integer = 0 To lv.Groups.Count
                Dim group As New LVGROUP()
                group.cbSize = Marshal.SizeOf(group)
                group.state = CInt(state)
                ' LVGS_COLLAPSIBLE 
                group.mask = 4
                ' LVGF_STATE 
                group.iGroupId = i
                Dim ip As IntPtr = IntPtr.Zero
                Try
                    ip = Marshal.AllocHGlobal(group.cbSize)
                    Marshal.StructureToPtr(group, ip, False) ' True is redundant  implemented by Finally and FreeHGlobal
                    SendMessage(lv.Handle, &H1000 + 147, i, ip)
                Catch ex As Exception
                    System.Diagnostics.Trace.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace)
                Finally
                    If ip <> IntPtr.Zero Then
                        Marshal.FreeHGlobal(ip)
                    End If
                End Try
            Next
        End Sub
    End Class
End Namespace
